/**
 * BatchMngtAction.java - 2012-2-10
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.batchsys.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lijin.batchsys.framework.common.BatchCache;
import com.lijin.batchsys.framework.common.BatchUtil;
import com.lijin.batchsys.framework.dao.model.BatchInfo;
import com.lijin.batchsys.framework.dao.model.JobInfo;
import com.lijin.batchsys.framework.dao.model.StepInfo;
import com.lijin.batchsys.web.bean.PageInfo;
import com.lijin.batchsys.web.service.BatchMngtService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Description: 批量控制Action
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class BatchMngtAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private BatchMngtService batchMngtService;
	
	private List<BatchInfo> batchInfoList = null;
	private List<JobInfo> jobInfoList = null;
	private List<StepInfo> stepInfoList = null;
    private String jobType = null;
    /**
	 * @return the batchNoShow
	 */
	public String getBatchNoShow() {
		return batchNoShow;
	}
	private String batchNoShow=null;

    private PageInfo<BatchInfo> schedulePageInfo = null;
    private PageInfo<JobInfo>   jobPageInfo = null;
    private PageInfo<StepInfo>  stepPageInfo = null;
    
    private int curPageNo = 1;
    

	public int getCurPageNo() {
		return curPageNo;
	}

	public void setCurPageNo(int curPageNo) {
		this.curPageNo = curPageNo;
	}
	
    
	public void setBatchMngtService(BatchMngtService batchMngtService) {
		this.batchMngtService = batchMngtService;
	}
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String scheduleCtrl() {
		return "scheduleCtrl";
	}
	public String jobCtrl() {
		return "jobCtrl";
	}
	public String stepCtrl() {
		return "stepCtrl";
	}
	public String logCtrl() {
		return "logCtrl";
	}
	public String jobMngt() {
		return "jobMngt";
	}
	public String scheduleMngt() {
		return "scheduleMngt";
	}
	public String stepMngt() {
		return "stepMngt";
	}
	
	
	/**
	 * 获取Batch的信息列表
	 * @return
	 */
	public String listBatchInfo() {
		schedulePageInfo = new PageInfo<BatchInfo>(curPageNo);
		batchMngtService.buildbatchPageInfo(schedulePageInfo);
		
		return "batchs";
	}
	
	
	

	/**
	 * 获取Job信息列表
	 * @return
	 */
	public String listJobInfo() {
		jobPageInfo = new PageInfo<JobInfo>( curPageNo);
		batchMngtService.buildJobPageInfo(jobPageInfo);
		
		return "jobs";
	}
	
	/**
	 * 获取Step信息列表
	 * @return
	 */
	public String listStepInfo() {
		System.out.print("curPageNo"+curPageNo);
		stepPageInfo = new PageInfo<StepInfo>(curPageNo);
		batchMngtService.buildStepPageInfo(stepPageInfo);
		
		return "steps";
	}
	
	/**
	 * 获取拉起的步骤列表
	 * @return
	 */
	public String listStep2Run() {
		 listStepInfo();
		
		return "steps2Run";
	}
	
	/**
	 * 获取拉起的步骤列表
	 * @return
	 */
	public String listJob2Run() {
		listJobInfo();
		
		return "jobs2Run";
	}
	
	/**
	 * 通过jobNo获得对应的stepList
	 * @return
	 */
	public String stepsFromJob() {
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("jobNo="+request.getParameter("jobNo"));
		System.out.println("batchNo="+request.getParameter("batchNo"));
		int jobNo = Integer.parseInt(request.getParameter("jobNo"));
		int batchNo = Integer.parseInt(request.getParameter("batchNo"));
		
		JobInfo job = BatchCache.getInstance().getJobInfo(jobNo, batchNo);
		stepInfoList = job.getStepList();
		for(StepInfo stepInfo : stepInfoList){
			stepInfo.setChStepCtrlFlgs(BatchUtil.stepCtrlFlgsToChStr(stepInfo));
			stepInfo.setChStepRunPeriod(BatchUtil.stepRunPeriodToChStr(stepInfo.getStepRunPeriod()));
		}
		
		jobType = job.isConcurrent() ? "并行" : "串行" ;
		
		return "jobstep";
	}
	
	
	/**
	 * 通过batchNo获得对应的jobList
	 * @return
	 */
	public String jobsFromBatch() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int batchNo = Integer.parseInt(request.getParameter("batchNo"));
		
		BatchInfo batch = BatchCache.getInstance().getBatchInfo(batchNo);
		jobInfoList = batch.getJobList();
		for(JobInfo jobInfo : jobInfoList){
			jobInfo.setChJobCtrlFlgs(BatchUtil.jobCtrlFlgsToChStr(jobInfo));
		}
		batchNoShow=batchNo+"";
		return "batchjob";
	}

	
	public List<BatchInfo> getBatchInfoList() {
		return batchInfoList;
	}
	public List<JobInfo> getJobInfoList() {
		return jobInfoList;
	}
	public List<StepInfo> getStepInfoList() {
		return stepInfoList;
	}
	public String getJobType() {
		return jobType;
	}
	public PageInfo<BatchInfo> getSchedulePageInfo() {
		return schedulePageInfo;
	}
	public PageInfo<JobInfo> getJobPageInfo() {
		return jobPageInfo;
	}
	public PageInfo<StepInfo> getStepPageInfo() {
		return stepPageInfo;
	}
	
}